package com.onlyphoto.onlymall.mvp.view.activity

import android.view.View
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseActivity
import com.onlyphoto.onlymall.bean.request.ResetPwdBean
import com.onlyphoto.onlymall.listeners.NoDoubleClickListener
import com.onlyphoto.onlymall.mvp.contract.CResetPwd
import com.onlyphoto.onlymall.mvp.presenter.PResetPwdImpl
import com.onlyphoto.onlymall.utils.AndroidUtils
import com.onlyphoto.onlymall.utils.Constants
import com.onlyphoto.onlymall.utils.SharePreferenceUtil
import com.onlyphoto.onlymall.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_resetpwd.*

class ResetPwdActivity : BaseActivity<PResetPwdImpl>(), CResetPwd.IVResetPwd {

    var phone = ""

    override fun retry() {}

    override fun init() {
        phone = intent.getStringExtra("phone")!!

        //判断验证码是否需要继续计时
        val overTime = SharePreferenceUtil.getInstance(this).getLong(Constants.LOGIN_CODE_FINISH_TIME, 0)
        if (System.currentTimeMillis() < overTime) {
            //需要继续计时
            val remainderTime: Long = overTime - System.currentTimeMillis()
            tv_verification.startCountDownTime(remainderTime)
        } else {
            mPresenter?.getVerify(3, phone)
        }
    }

    override fun initListener() {
        AndroidUtils.isButtonEnable(intArrayOf(4, 6, 6), commit, et_sms_code, et_pwd, pwd_confirm)

        back.setOnClickListener{
            finish()
        }

        val listener = object: NoDoubleClickListener(){
            override fun onNoDoubleClick(view: View?) {
                when (view?.id) {
                    R.id.commit -> {
                        mPresenter!!.resetPwd()
                    }
                    R.id.tv_verification -> {
                        tv_verification.setText("正在发送",false)
                        mPresenter!!.getVerify(3, phone)
                        //持久化验证码倒计时
                        SharePreferenceUtil.getInstance(this@ResetPwdActivity).setLong(Constants.LOGIN_CODE_FINISH_TIME, System.currentTimeMillis() + Constants.CODE_OVER_TIME * 1000)
                        //保存号码
                        SharePreferenceUtil.getInstance(this@ResetPwdActivity).setData(Constants.SHARE_TAG_LOGIN_PHONE, phone)
                    }
                }
            }
        }

        tv_verification.setOnClickListener(listener)
        commit.setOnClickListener(listener)
    }

    override fun getResetPwdBean(): ResetPwdBean {
        val bean = ResetPwdBean()
        bean.post_type = 3
        bean.msg_code = et_sms_code.text.toString()
        bean.phone = phone
        bean.new_password = et_pwd.text.toString()
        return bean
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    override fun getVerifySuccess() {
        tv_verification.startCountDownTime()
        ToastUtil.showToast("验证码已发送")
    }

    override fun getVerifyFail() {
        tv_verification.reset()
        ToastUtil.showToast("验证码发送失败")
    }

    override fun resetPwdSuccess() {
        showMsg("密码已重置")
        finish()
    }

    override fun loadData() {
        //加载数据
    }

    override fun isFullScreen(): Boolean {
        //是否沉浸式， 默认非沉浸式
        return false
    }

    public override fun initCustomerStatusManager() {
    }

    override fun setContentViewId(): Int {
        return R.layout.activity_resetpwd
    }

    override fun createPresenter() {
        mPresenter = PResetPwdImpl(mContext, this)
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

}
